/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.risk.control.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hopes.bizz.risk.common.constant.enums.RiskModDictEnum;
import com.hopes.bizz.risk.control.bo.RiskUnitStopBO;
import com.hopes.bizz.risk.control.entity.RiskObjectEntity;
import com.hopes.bizz.risk.control.service.RiskObjectService;
import com.hopes.bizz.risk.control.service.RiskUnitService;
import com.hopes.bizz.risk.control.vo.RiskObjectVO;
import com.hopes.bizz.risk.control.vo.RiskUnitStopVO;
import com.hopes.bizz.risk.control.vo.RiskUnitVO;
import com.hopes.boss.admin.api.entity.SysDictItem;
import com.hopes.boss.admin.api.resolver.DictResolver;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.log.annotation.SysLog;
import com.hopes.bizz.risk.control.entity.RiskUnitStopEntity;
import com.hopes.bizz.risk.control.dto.RiskUnitStopExcelDTO;
import com.hopes.bizz.risk.control.vo.RiskUnitStopExcelVO;
import com.hopes.bizz.risk.control.service.RiskUnitStopService;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import com.hopes.boss.common.excel.annotation.RequestExcel;
import com.hopes.boss.common.excel.annotation.ResponseExcel;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.springframework.http.HttpHeaders;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 装置停用检维修记录
 *
 * @author cx
 * @date 2023-07-17 19:39:27
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/riskUnitStop" )
@Tag(description = "riskUnitStop" , name = "装置停用检维修记录管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RiskUnitStopController {

    private final RiskUnitStopService riskUnitStopService;

    private final RiskUnitService riskUnitService;





    /**
     * 分页查询装置停用检维修记录
     * @param page 分页对象
     * @param riskUnitStopBO
     * @return R
     */
    @Operation(summary = "查询装置停用检维修记录" , description = "查询装置停用检维修记录" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('control_risk_unit_stop_view')" )
    public R getRiskUnitStopPage(Page page, RiskUnitStopBO riskUnitStopBO) {
        return R.ok(riskUnitStopService.getRiskUnitStopPage(page,riskUnitStopBO));
    }


    /**
     * 通过id查询装置停用检维修记录
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('control_risk_unit_stop_view')" )
    public R getById(@PathVariable("id" ) String id) {
        Object riskObjectVo = riskUnitStopService.getRiskObjectById(id);
        return R.ok(riskObjectVo);
    }

    /**
     * 新增装置停用检维修记录
     * @param riskUnitStop 装置停用检维修记录
     * @return R
     */
    @Operation(summary = "新增装置停用检维修记录" , description = "新增装置停用检维修记录" )
    @SysLog("新增装置停用检维修记录" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('control_risk_unit_stop_add')" )
    public R save(@RequestBody RiskUnitStopEntity riskUnitStop) {
        SysDictItem riskUnit = DictResolver.getItemByValue(RiskModDictEnum.risk_unit, riskUnitStop.getRiskUnitId().toString());
        String riskObjectId = riskUnit.getExt2();
        SysDictItem riskObjects = DictResolver.getItemByValue(RiskModDictEnum.risk_object, riskObjectId);
        riskUnitStop.setOrgId(Long.valueOf(riskObjects.getExt2()));
        return R.ok(riskUnitStopService.save(riskUnitStop));
    }

    /**
     * 修改装置停用检维修记录
     * @param riskUnitStop 装置停用检维修记录
     * @return R
     */
    @Operation(summary = "修改装置停用检维修记录" , description = "修改装置停用检维修记录" )
    @SysLog("修改装置停用检维修记录" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('control_risk_unit_stop_edit')" )
    public R updateById(@RequestBody RiskUnitStopEntity riskUnitStop) {
        return R.ok(riskUnitStopService.updateById(riskUnitStop));
    }

    /**
     * 通过id删除装置停用检维修记录
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除装置停用检维修记录" , description = "通过id删除装置停用检维修记录" )
    @SysLog("通过id删除装置停用检维修记录" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('control_risk_unit_stop_del')" )
    public R removeById(@RequestBody String[] ids) {
        return R.ok(riskUnitStopService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * excel模板
     *
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/template")
    @PreAuthorize("@pms.hasPermission('control_risk_unit_stop_export')")
    public List<RiskUnitStopExcelVO> template() {
        return riskUnitStopService.exportExcel(Arrays.asList(new RiskUnitStopEntity()));
    }

    /**
     * 导出excel 表格
     * @param riskUnitStopExcelDto 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('control_risk_unit_stop_export')")
    public List<RiskUnitStopExcelVO> export(RiskUnitStopExcelDTO riskUnitStopExcelDto) {
        return riskUnitStopService.exportExcel(riskUnitStopService.list());
    }

    /**
     * 导入excel 表格
     * @param riskUnitStopExcelDtoList 查询条件
     * @param bindingResult 错误信息列表
     * @return ok fail
     */
    @PostMapping("/import")
    @PreAuthorize("@pms.hasPermission('control_risk_unit_stop_export')")
    public R importExcel(@RequestExcel List<RiskUnitStopExcelDTO> riskUnitStopExcelDtoList, BindingResult bindingResult) {
        return riskUnitStopService.importExcel(riskUnitStopExcelDtoList, bindingResult);
    }
}